#include<func.h>

int main (int argc,char*argv[])
{
    ARGS_CHECK(argc,3);
    int SFd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(SFd,-1,"socket");
    int optval =1;
    int ret = setsockopt(SFd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(int));//不需要那个waittime
    ERROR_CHECK(ret,-1,"setsockopt");
    struct sockaddr_in serAddr;
   
   serAddr.sin_family = AF_INET;
   serAddr.sin_port = htons(atoi(argv[2]));
   serAddr.sin_addr.s_addr = inet_addr(argv[1]);
   //服务器的接收需要 bind listen accept;
    ret = bind(SFd,(struct sockaddr*)&serAddr,sizeof(serAddr));
   ERROR_CHECK(ret,-1,"bind");
   ret = listen(SFd,10);
  ERROR_CHECK(ret,-1,"listen");
  int newFd  = accept(SFd,NULL,NULL);
  ERROR_CHECK(ret,-1,"accept");
  fd_set rdset;
  char buf [4096] = {0};
  while(1)
  {
    FD_ZERO(&rdset);
    FD_SET(newFd,&rdset);
    FD_SET(STDIN_FILENO,&rdset);
    ret = select(newFd+1,&rdset,NULL,NULL,NULL);
    ERROR_CHECK(ret,-1,"select");
    if(FD_ISSET(newFd,&rdset))
    {
        memset(buf,0,sizeof(buf));
       ret=recv(newFd,buf,sizeof(buf),0);
       if(ret==0)
       {
        printf("你是个好人");
        break;
       }
       puts(buf);
    }
    if(FD_ISSET(STDIN_FILENO,&rdset))
    {
        memset(buf,0,sizeof(buf));
   int ret=    read(STDERR_FILENO,buf,sizeof(buf));
   if(ret==0)
   {
       printf("end\n");
    break;
   }
   send(newFd,buf,strlen(buf),0);
    }
  }
  close(SFd);
  close(newFd);
  return 0;
}